@
@
@atambalasi2016-02-12 09:36:39
WordPress
@atambalasi, 2016-02-12 09:36:39

Wordpress why when permalink is clicked sends a link to the main page?

There is a page where all the news is displayed (mini picture and news quote).

<?php $temp = $wp_query; $wp_query= null;
          $wp_query = new WP_Query(); $wp_query->query('&cat=2&showposts=3' . '&paged='.$paged);
          while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
          <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 each-news">
          <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12 news-name"> <a href="#"><?php the_title(); ?> </a></div>
          <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 news-thumbnail">
            <a href="<?php the_permalink(); ?>"><?php  the_post_thumbnail() ?></a> 
          </div>
          <div class="col-lg-8 col-md-7  col-sm-8 col-xs-12 news-shortext">

            <a href="<?php the_permalink(); ?>"><p>
              <?php the_excerpt(); ?>
            </p></a>
          </div>
          <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 news-info">
            <p class="news-date-published"><span>Создан: </span><?php echo get_the_date(); ?></p>
            <p class="author-news"><span>Автор: </span>  <?php the_author(); ?> </p>
          </div>
        </div>

How can I make it show the full news item when clicked? Now when clicked, it sends to the main page.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-02-12
@HeadOnFire

Is this a secondary loop or are you trying to replace the main one entirely? In both cases, you are doing it wrong.
1. If this is a secondary loop (which goes separately from the main one), we write in the desired template:

<?php
/**
 * Secondary loop
 */

// Устанавливаем нужные аргументы
$args = array(
    'cat' => 2,
    'posts_per_page' => 3, // showposts - устаревший аргумент, с версии 2.1
);
// Выполняем запрос
$posts = new WP_Query( $args );

// Если посты найдены
if( $posts->have_posts() ) :

    // Цикл для вывода постов
    while( $posts->have_posts() ) : 
        // Закидываем текущий пост в цикле в глобальную переменную $post, чтобы работали все template tags
        $posts->the_post();

        // Тут вывод данных

    endwhile; // Конец цикла
    wp_reset_postdata(); // Обнуляем данные произвольного цикла и возвращаемся к основному loop

else :

// Посты не найдены

endif;

2. If you need to modify the main loop, write to functions.php:
// Функция, которую хукаем и модифицируем запрос
function my_modify_query( $query ) {

    // Выполняем модификацию только если это основной запрос и мы находимся НЕ в админке
    if( $query->is_main_query() && ! $query->is_admin() ) {
        $query->set( 'cat', 2 );
        $query->set( 'posts_per_page', 3 );
    }

}
// Собственно хук в action 'pre_get_posts', который выполняется когда все параметры запроса установлены, но сам запрос в БД еще не ушел
add_action( 'pre_get_posts', 'my_modify_query' );

There is no need to fiddle around with $paged, WordPress will handle everything correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question