Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z funkcją query w Wordpressie
Tonio
post
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 10.01.2008

Ostrzeżenie: (0%)
-----


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?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
memory
post
Post #2





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


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

Ten post edytował memory 30.09.2015, 19:24:32
Go to the top of the page
+Quote Post
webwinner
post
Post #3





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 20.05.2015

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 17:12