Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Problem z funkcją query w Wordpressie
Forum PHP.pl > Forum > PHP
Tonio
Mam problem z funkcją query przy postach w wordpressie. Możliwe, że to moja wina (pewnie tak), ale dzieją się rzeczy dla mnie niezrozumiałe.

Już dwukrotnie zmieniałem kod, ale za każdym razem coś nie funkcjonuje. Najpierw WP wyświetlał wpisy z wszystkich kategorii zamiast tych, które ustaliłem, że mają być wyświetlane (czyli 2 i 3). Tak wyglądał kod:

CODE
<div id="news">

<?php
// the query
$the_query = new WP_Query( array('cat=2,3', 'posts_per_page=1') ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<!-- pagination here -->

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>



<div class="news">


<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'single-post', array('class' => 'news-glowny') ); } ?>


<div class="news-opis">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php if (in_category('futnet')) { ?>
<img src="http://XXXX/grafika/1.png">
<?php } elseif (in_category('footvolley')) { ?>
<img src="http://XXXX/grafika/2.png">
<?php } ?>
</div>

</div>





<?php endwhile; ?>
<!-- end of the loop -->

<!-- pagination here -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>


<ul id="wiadomosci">
<?php
// the query
$the_query = new WP_Query( array( 'cat=2,3', 'posts_per_page=2', 'offset' => 1) ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<!-- pagination here -->

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>



<div class="news">


<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'single-post', array('class' => 'news-glowny') ); } ?>


<div class="galeria-opis">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php if (in_category('futnet')) { ?>
<img src="http://XXXX/grafika/1.png">
<?php } elseif (in_category('footvolley')) { ?>
<img src="http://XXXX/grafika/2.png">
<?php } ?>
</div>

</div>





<?php endwhile; ?>
<!-- end of the loop -->

<!-- pagination here -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>

</div>


Potem dokonałem zmiany przy query, ale stało się coś jeszcze dziwniejszego. Mam dwa pola do wyświetlania newsów. Na razie w żadnym z nich nie powinien się pojawić żaden news, bo nie dodałem go w kategoriach 2 i 3. Tymczasem pojawiły się te z innych kategorii w polu, w którym w ogóle nie dałem query...

CODE
<div id="news">

<?php query_posts('cat=2,3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>



<div class="news">


<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'single-post', array('class' => 'news-glowny') ); } ?>


<div class="news-opis">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php if (in_category('futnet')) { ?>
<img src="http://XXXX/grafika/1.png">
<?php } elseif (in_category('footvolley')) { ?>
<img src="http://XXXX/grafika/2.png">
<?php } ?>
</div>

</div>





<?php endwhile; endif; ?><?php wp_reset_query(); ?>


<ul id="wiadomosci">




<div class="news">


<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'single-post', array('class' => 'news-glowny') ); } ?>


<div class="galeria-opis">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php if (in_category('futnet')) { ?>
<img src="http://XXXX/grafika/1.png">
<?php } elseif (in_category('footvolley')) { ?>
<img src="http://XXXX/grafika/2.png">
<?php } ?>
</div>

</div>




</ul>

</div>


Newsy wyświetliły się w sekcji "ul id="wiadomosci"". Wiem, że karkołomne rozwiązanie (tu divy, tu ul itd.), ale w tym fragmencie nie powinno być wg mnie żadnego newsa.

Jak mogę rozwiązać ten kłopot? Dlaczego funkcja query nie daje wyniku w postaci newsów z kategorii 2 i 3 (czyli puste pole), a wszystkich newsów, które zostały w ogóle dodane?
memory
Powinno być
  1. $query = new WP_Query( array( 'cat' => '2,6,17,38', 'posts_per_page'=>'1' ) );


a u ciebie jest

  1. $the_query = new WP_Query( array('cat=2,6,17,38', 'posts_per_page=1') ); ?>


Poczytaj troche o tablicach snitch.gif
array
webwinner
Tak jak kolega wyżej napisał tutaj jest problem z poprawnym zapisem kodu.

Tutaj masz rozpisane dokładnie jak używać:

https://codex.wordpress.org/Class_Reference/WP_Query
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.